package com.sc.nft.mapstruct;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.MallDrawActivityBase;
import com.sc.nft.entity.MallDrawActivityPrizePool;
import com.sc.nft.entity.MallDrawActivityPrizePoolDetail;
import com.sc.nft.entity.MallDrawActivityProp;
import com.sc.nft.entity.dto.draw.activity.AutoReplenishmentDTO;
import com.sc.nft.entity.dto.draw.activity.MallDrawActivityDTO;
import com.sc.nft.entity.dto.draw.activity.MallDrawActivityPrizePoolDTO;
import com.sc.nft.entity.dto.draw.activity.MallDrawActivityPrizePoolDetailDTO;
import com.sc.nft.entity.dto.draw.activity.MallDrawActivityPropDTO;
import com.sc.nft.entity.vo.draw.activity.MallDrawActivityBaseVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper(componentModel = "spring")
public interface MallDrawActivityMapStruct {

    MallDrawActivityMapStruct INSTANCE = Mappers.getMapper(MallDrawActivityMapStruct.class);

    Page<MallDrawActivityBaseVO> mallDrawActivityBase2VOPage(Page<MallDrawActivityBase> page);

    MallDrawActivityBase mallDrawActivityDTO2DO(MallDrawActivityDTO mallDrawActivityDTO);

    MallDrawActivityDTO mallDrawActivityBase2DTO(MallDrawActivityBase mallDrawActivityBase);

    MallDrawActivityPrizePoolDTO mallDrawActivityPrizePool2DTO(MallDrawActivityPrizePool mallDrawActivityPrizePool);

    MallDrawActivityPrizePool mallDrawActivityPrizePoolDTO2DO(MallDrawActivityPrizePoolDTO mallDrawActivityPrizePoolDTO);

    MallDrawActivityPrizePoolDetail mallDrawActivityPrizePoolDetailDTO2DO(MallDrawActivityPrizePoolDetailDTO mallDrawActivityPrizePoolDetailDTO);

    List<MallDrawActivityPrizePool> mallDrawActivityPrizePoolDTO2DO(List<MallDrawActivityPrizePoolDTO> mallDrawActivityPrizePoolList);

    List<MallDrawActivityPrizePoolDetail> mallDrawActivityPrizePoolDetailDTO2DO(List<MallDrawActivityPrizePoolDetailDTO> mallDrawActivityPrizePoolDetailDTOList);

    List<MallDrawActivityProp> mallDrawActivityPropDTO2DO(List<MallDrawActivityPropDTO> mallDrawActivityPropDTOList);

    List<MallDrawActivityPropDTO> mallDrawActivityPropList2DTO(List<MallDrawActivityProp> mallDrawActivityPropList);

    @Mapping(source = "autoReplenishmentJson", target = "autoReplenishmentDTO", qualifiedByName = "jsonToDTO")
    MallDrawActivityPrizePoolDetailDTO drawActivityPrizePoolDetail2DTO(MallDrawActivityPrizePoolDetail drawActivityPrizePoolDetail);


    List<MallDrawActivityPrizePoolDetailDTO> mallDrawActivityPrizePoolDetail2DTO(List<MallDrawActivityPrizePoolDetail> drawActivityPrizePoolDetailList);

    @Named("jsonToDTO")
    default AutoReplenishmentDTO jsonToDTO(String autoReplenishmentJson) {
        if (autoReplenishmentJson == null || autoReplenishmentJson.trim().isEmpty()) {
            return null;
        }
        try {
            return JSON.parseObject(autoReplenishmentJson, AutoReplenishmentDTO.class);
        } catch (Exception e) {
            throw new RuntimeException("Failed to convert autoReplenishmentJson to AutoReplenishmentDTO", e);
        }
    }
}
